﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using wne.Context;
using wne.core;
using wne.models;
using Wne.SenderService.GlobalStorage;
using Timer = System.Timers.Timer;
namespace Wne.SenderService
{
    public partial class Service1 : ServiceBase
    {
        private Timer _issuerTimer;
        private ThreadSemaphore[] _issuerThreads;
        private int _threadCount = 3;
        private readonly IssueThreadManager _core;
        public Service1()
        {
            InitializeComponent();
            _core = new IssueThreadManager(_threadCount);
        }

        private void IssuerTimer_elapsed(object sender, System.Timers.ElapsedEventArgs args)
        {
            _core.IssuerElapsed(_issuerThreads);
        }
        
        
        protected override void OnStart(string[] args)
        {
            _core.IssuerInit(_issuerThreads);
            _issuerTimer = new Timer { Interval = 1000 };
            _issuerTimer.Elapsed += IssuerTimer_elapsed;
            _issuerTimer.Start();
        }

        protected override void OnStop()
        {
            _issuerThreads = new ThreadSemaphore[_threadCount];
            _core.IssuerStop(_issuerThreads);
        }
    }
    public class ThreadSemaphore
    {
        public IssueSemaphore Semaphore { get; set; }
        public Thread CurrentThread { get; set; }

        public ThreadSemaphore(Thread thread)
        {
            CurrentThread = thread;
        }
    }
    public class IssueSemaphore
    {
        [DefaultValue(true)]
        public bool GreenLight { get; set; }
    }
    public class IssueThreadManager
    {
        private readonly int _threadCount;

        public IssueThreadManager(int threadCount)
        {
            _threadCount = threadCount;
        }
        public void IssuerInit(ThreadSemaphore[] issuerThreads)
        {
            for (int i = 0; i < _threadCount; i++)
            {
                var thread = new Thread(semaphore => (new IssueManager()).TryIssue((IssueSemaphore)semaphore));
                issuerThreads[i] = new ThreadSemaphore(thread);
                var semaphor = new IssueSemaphore();
                thread.Start(semaphor);
                issuerThreads[i].Semaphore = semaphor;
            }
        }

        public void IssuerStop(ThreadSemaphore[] issuerThreads)
        {
            Parallel.ForEach(issuerThreads, thread =>
            {
                lock (thread.Semaphore)
                {
                    thread.Semaphore.GreenLight = false;
                }
                thread.CurrentThread.Join();
            });
        }

        public void IssuerElapsed(ThreadSemaphore[] issuerThreads)
        {
            for (int i = 0; i < _threadCount; i++)
            {
                Console.WriteLine(issuerThreads[i].CurrentThread.ManagedThreadId + " Закончился?");
                if (issuerThreads[i].CurrentThread.ThreadState != ThreadState.Running)
                {
                    issuerThreads[i] =
                        new ThreadSemaphore(new Thread(semaphore => new IssueManager().TryIssue((IssueSemaphore)semaphore)));
                    var semaphor = new IssueSemaphore();
                    issuerThreads[i].CurrentThread.Start(semaphor);
                    issuerThreads[i].Semaphore = semaphor;
                    Console.WriteLine(issuerThreads[i].CurrentThread.ManagedThreadId + " Закончился, запустили!");
                }
                else
                {
                    Console.WriteLine(issuerThreads[i].CurrentThread.ManagedThreadId + " Не закончился!");
                }
            }
        }
    }

    
}
